/*
 * Copyright 2008 Andrej Herich
 * 
 * http://azetclient.yw.sk/
 * http://azetclient.googlecode.com/
 * 
 * This file is part of AzetClient.
 * 
 * AzetClient is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * AzetClient is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with AzetClient.  If not, see <http://www.gnu.org/licenses/>.
 */

package sk.yw.azetclient.gui;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.table.AbstractTableModel;
import sk.yw.azetclient.Main;
import sk.yw.azetclient.model.Buddy;
import sk.yw.azetclient.model.Message;
import sk.yw.azetclient.model.MessageThread;

/**
 *
 * @author error216
 */
public class ConversationListModel extends AbstractTableModel {
    
    private static final long serialVersionUID = -933865838L;
    
    private Buddy user;
    private List<MessageThread> threads;
    private List<Buddy> threadBuddies = new ArrayList<Buddy>();
    
    public ConversationListModel(Buddy user, List<MessageThread> threads) {
        if (user == null)
            throw new IllegalArgumentException("Null pointer in user");
        if (threads == null)
            throw new IllegalArgumentException("Null pointer in threads");
        
        this.user = user;
        this.threads = threads;
        
        for (MessageThread thread : threads) {
            Message message = thread.getMessages().get(0);
            Buddy buddy;
            if (user.equals(message.getSender())) {
                buddy = message.getReceiver();
            } else {
                buddy = message.getSender();
            }
            
            Buddy buddy1 = Main.getMainFrame().getBuddy(buddy.getName());
            if (buddy1 != null) {
                threadBuddies.add(buddy1);
            } else {
                buddy1 = Main.getMainFrame().getOther(buddy.getName());
                if (buddy1 != null) {
                    threadBuddies.add(buddy1);
                } else {
                    threadBuddies.add(buddy);
                }
            }
        }
    }

    public int getRowCount() {
        return threads.size();
    }

    public int getColumnCount() {
        return 3;
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        if (columnIndex == 0) {
            return threadBuddies.get(rowIndex);
        } else if (columnIndex == 1) {
            MessageThread thread = threads.get(rowIndex);
            if (thread == null) return null;
            else {
                Message lastMessage = thread.getLastMessage();
                if (lastMessage == null) return null;
                else {
                    return lastMessage.getSendTime();
                }
            }
        } else if (columnIndex == 2) {
            return threads.get(rowIndex);
        }
        throw new IndexOutOfBoundsException("columnIndex out of bounds: " + columnIndex);
    }
    
    @Override
    public Class<?> getColumnClass(int columnIndex) {
        if (columnIndex == 0) {
            return Buddy.class;
        } else if (columnIndex == 1) {
            return Date.class;
        } else if (columnIndex == 2) {
            return MessageThread.class;
        }
        throw new IndexOutOfBoundsException("columnIndex out of bounds: " + columnIndex);
    }
    
    @Override
    public String getColumnName(int columnIndex) {
        if (columnIndex == 0) {
            return Main.getText("conversationListFrame.buddy");
        } else if (columnIndex == 1) {
            return Main.getText("conversationListFrame.time");
        } else if (columnIndex == 2) {
            return Main.getText("conversationListFrame.conversation");
        }
        throw new IndexOutOfBoundsException("columnIndex out of bounds: " + columnIndex);
    }

}
